//
//  ContentView.swift
//  MagaCore
//
//  Created by MrMessy on 2023/4/28.
//

import SwiftUI
import CoreData
import ReSwift
import AdSupport
import PKHUD
import AppTrackingTransparency
import AdSupport

class HTPathManager: ObservableObject {
   @Published var var_path:[HTPathRout] = [] // Hashable 序列
}

struct HTContentView: View {

  @EnvironmentObject private var var_store: HTAppStore
  @State private var var_isFirstOpen: Bool = false
    // 是否同意通知
  @State private var var_isAgreeNotice: Bool = false
  @State private var var_showPremiumGuide: Bool = false
  @State private var var_showTabBar: Bool = false
  @State private var var_selectIndex: Int = 0
  @State private var var_curSubscribe: HTSubscribe?
  @State private var var_colorScheme: ColorScheme = .light
 
  var body: some View {
    ZStack {
      if (!var_isAgreeNotice) {
          // 没同意，每次启动都需要弹出
        HTPermissionScreen {
          var_isAgreeNotice = true
          var_showTabBar = true
          ht_loadDate()
        }
      } else {
          // TabBar
        HTTabBar(var_selectIndex: $var_selectIndex)
      }

      if (var_showPremiumGuide) {
        HTPremiumGuideScreen {
          var_showPremiumGuide = false
          HTNetworkManager().ht_requestForBuried(.htEnumVipSh(var_source: "37"))
          NotificationCenter.default.post(name: HTConfig.STATIC_notification_interstitialAd, object: nil, userInfo: ["status": "canshow"])
          NotificationCenter.default.post(name: HTConfig.STATIC_notification_showEpisodesPopupView, object: nil, userInfo: ["status": "show"])
        } var_onChooseMore: {
          var_showPremiumGuide = false
          var_selectIndex = 1
          NotificationCenter.default.post(name: HTConfig.STATIC_notification_interstitialAd, object: nil, userInfo: ["status": "canshow"])
          NotificationCenter.default.post(name: HTConfig.STATIC_notification_showEpisodesPopupView, object: nil, userInfo: ["status": "show"])
        }
      }
    }
    .preferredColorScheme(var_colorScheme)
    .onReceive(NotificationCenter.default.publisher(for: HTConfig.STATIC_notification_colorScheme)) { notification in
      if let userInfo = notification.userInfo {
        let value = userInfo["key"] as! String
        if (value == "dark") {
          var_colorScheme = .dark
        } else if (value == "light") {
          var_colorScheme = .light
        }
      }
    }
    .onAppear{
      let firstOpen = UserDefaults().string(forKey: HTConfig.STATIC_userDefaults_firstOpen)
      if (firstOpen != "false") {
        UserDefaults().set("false", forKey: HTConfig.STATIC_userDefaults_firstOpen)
      }

      let agreeNotice = UserDefaults().string(forKey: HTConfig.STATIC_userDefaults_agreeNotice)
      if (agreeNotice == "true") {
        var_isAgreeNotice = true
      }

      ht_loadDate();

      ATTrackingManager.requestTrackingAuthorization {status in
        print("请求IDFA状态： \(status)");
        if status == .authorized {
          print("获取到IDFA： \(ASIdentifierManager.shared().advertisingIdentifier)");
        }
      }
    }

  }

  func ht_loadDate() {
    if (var_isAgreeNotice) {
      var_store.dispatch(HTEnumAdAction.ht_adConfig())
      var_store.dispatch(HTEnumSubscribeAction.ht_retrieveProductsInfo())

      let userExpiredDate = UserDefaults().string(forKey: HTConfig.STATIC_userDefaults_userExpiredDate)
      if (userExpiredDate == nil) {
        var_showPremiumGuide = true
      } else {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
          NotificationCenter.default.post(name: HTConfig.STATIC_notification_showEpisodesPopupView, object: nil, userInfo: ["status": "show"])
        }
        var_store.dispatch(HTEnumSubscribeAction.ht_getCurrentSubscriptionInfo(callback: nil))
      }
    }
  }
}
